angular.module('oblique-features')
.factory 'featuresService', ['$log', '$http', '$interval', ($log, $http, $interval) ->
new class FeaturesService
fileSource: null
intervalId: null
baseRequiredForAllFeatures: false
featuresEnabledStatus:
base: true
constructor: ->
@setFeatureEnabledData {}
checkFeatureEnabled: (featureName) ->
if not featureName? then featureName = 'base'
if @baseRequiredForAllFeatures
if not @featuresEnabledStatus[featureName]?
@featuresEnabledStatus['base']
else
@featuresEnabledStatus['base'] && @featuresEnabledStatus[featureName]
else
if @featuresEnabledStatus[featureName]? then @featuresEnabledStatus[featureName] else true
toggleFeature: (featureName) ->
@featuresEnabledStatus[featureName] = not @featuresEnabledStatus[featureName]
setFeatureEnabledData: (data) =>
if not data.base? then data.base = true
@featuresEnabledStatus = data
setBaseFeatureRequiredForAllFeatures: (reqdForAll) =>
@baseRequiredForAllFeatures = reqdForAll
setRemoteSource: (sourceURI, repeatIn, repeatCount) ->
@fileSource = sourceURI
Iif repeatIn?
@intervalId = $interval(@checkRemoteSource, repeatIn, repeatCount)
if repeatIn < 60000 then $log.warn 'Warning: Checking source at extremely short intervals is not advised'
@checkRemoteSource()
checkRemoteSource: =>
return $http.get(@fileSource)
.then (response) =>
@setFeatureEnabledData response.data
console.log 'Returning: ', response.data
return response.data
.catch (errors) ->
$log.error "Errors encountered retrieving feature status information: ", errors
setHardCodedSource: (data) =>
@setFeatureEnabledData data
if @intervalId?
$interval.cancel @intervalId
]
|